/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.anny.prodcuct.service;

import java.util.List;
import java.util.Map;

import com.jeeplus.modules.anny.prodcuct.entity.ProductSpecifications;
import com.jeeplus.modules.anny.prodcuct.mapper.ProductSpecificationsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.anny.prodcuct.entity.ProductCommodity;
import com.jeeplus.modules.anny.prodcuct.mapper.ProductCommodityMapper;

/**
 * 商品表(大)  用于描述商品的大体情况Service
 * @author zhengcq
 * @version 2018-10-17
 */
@Service
@Transactional(readOnly = true)
public class ProductCommodityService extends CrudService<ProductCommodityMapper, ProductCommodity> {

	@Autowired
	private ProductSpecificationsMapper productSpecificationsMapper;

	public ProductCommodity get(String id) {
		return super.get(id);
	}
	
	public List<ProductCommodity> findList(ProductCommodity productCommodity) {
		return super.findList(productCommodity);
	}
	
	public Page<ProductCommodity> findPage(Page<ProductCommodity> page, ProductCommodity productCommodity) {
		return super.findPage(page, productCommodity);
	}

	/**
	 * 查询会员或者用户可购买的商品
	 * @param map
	 * @return
	 */
	public List<Map<String,Object>> findVipOrEvaluate(ProductCommodity productCommodity){
		return super.mapper.findListByProperty(productCommodity);
	}
	@Transactional(readOnly = false)
	public void save(ProductCommodity productCommodity) {
		if (productCommodity.getIsNewRecord()){
			productCommodity.preInsert();
			mapper.insert(productCommodity);
		}else{
			productCommodity.preUpdate();
			mapper.update(productCommodity);
			ProductSpecifications productSpecifications = new ProductSpecifications();
			productSpecifications.setProductId(productCommodity.getId());
			String productName = productCommodity.getName();
			List<ProductSpecifications> list = productSpecificationsMapper.findList(productSpecifications);
			if (list != null && list.size() >0){
				for (ProductSpecifications p : list){
					p.setProductName(productName);
					productSpecificationsMapper.update(p);
				}
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ProductCommodity productCommodity) {
		super.delete(productCommodity);
	}
	
	/**
	 * 根据品类表id查找类别品牌下商品数量
	 * @param id
	 * @return
	 */
	public List<ProductCommodity> findListByCategoryId(String id){
		return mapper.findListByCategoryId(id);
	}
}